import { createRouter, createWebHashHistory} from 'vue-router'
import func from "@/common/func";

import Login from '../views/Login.vue'
import Index from '../views/Layout/Index.vue'

const routes = [
  {
    path: '/login',
    component: Login,
    hidden: true
  },
  {
    path: '/',
    component: Index,
    name: '总览',
    icon: 'HomeFilled',
    children: [
      { path: '/', component: () => import('../views/Home/Home.vue'), name: '总览'},
    ],
  },
  {
    path: '/content',
    component: Index,
    name: '内容管理',
    icon: '',
    children: [
      { path: '/content/banner', component: () => import('../views/Content/Banner.vue'), name: 'Banner管理', auth:'banner/bannerList' },
      { path: '/content/article', component: () => import('../views/Content/Article.vue'), name: '文章管理', auth:'article/articleList' },
      { path: '/content/config', component: () => import('../views/Content/Config.vue'), name: '配置管理', auth:'config/configList' },
    ],
  },
  {
    path: '/system',
    component: Index,
    name: '系统管理',
    icon: 'Tools',
    children: [
      { path: '/system/admin', component: () => import('../views/System/Admin.vue'), name: '管理员', auth:'system/adminList' },
      { path: '/system/auth', component: () => import('../views/System/Auth.vue'), name: '角色列表', auth:'system/authList' },
      { path: '/system/node', component: () => import('../views/System/Node.vue'), name: '节点列表', auth:'system/nodeTree' },
      { path: '/system/systemLog', component: () => import('../views/System/SystemLog.vue'), name: '系统日志', auth:'system/systemLog' },
    ],
  },
]

const router = new createRouter({
  history: createWebHashHistory(),
  base: '/admin/',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    func.delUser()
  }
  let user = func.getUser()
  if (!user.access_token && to.path !== '/login') {
    next({ path: '/login' });
  } else {
    next();
  }
});

export default router
